<?php namespace Admin\Controller; 
//后台首页控制器
use \Common\Model\Cate;
use \Common\Model\Goods;
use \Common\Model\Brand;
use \Common\Model\Type_attr;
use \Common\Model\Goods_attr;
use \Common\Model\Goods_detail;
use \Common\Model\Goods_list;
class GoodsController extends CommonController{
	private $model;
	protected function __auto(){
		$this->model=new Goods;
	}
    //显示商品
    public function index(){
       $data=$this->model->get();
	   View::with('data',$data);
       View::make();
    }
	//添加商品
	public function add(){
		if(IS_POST){
//			p(Q("post."));die;
			if(!$this->model->store()) View::error($this->model->getError());
			View::success("添加成功",U('index'));
		}
		//显示分类
       	$categoryMolde=new Cate;
	   	$cate=Data::tree($categoryMolde->get(),'cname');
	   	//显示品牌
	   	$brandModel=new Brand;
	   	$brand=$brandModel->get();
	   	View::with('brand',$brand);
	  	View::with('cate',$cate);
	   	View::make();
	}
	//uploadify异步上传
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
//	        p($file);
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
			
	    }
	}
	//uploadify异步删除图片
	public function delUpload(){
		$path=Q('post.path');
		unlink($path);
	}
	//异步获取类型属性
	public function addAttr(){
		//获取类型tid 
		$tid=Q('post.t',0,'intval');
		//查询shop_type_tid={$tid}的数据
		$typeAttrModel=new Type_attr;
		$data=$typeAttrModel->where("shop_type_tid={$tid}")->get();
		//将$data中的tavalue转换为数组
		foreach($data as $k=>$v){
			//将$data[$k]['tavalue']中的字符串转换为数组
			$data[$k]['tavalue']=explode('|', $v['tavalue']);
		}
		echo json_encode($data);exit;
	}
	//编辑商品
	public function edit(){
		if(IS_POST){
//p(Q('post.'));die;
			if(!$this->model->edit()) View::error($this->model->getError());
			
			View::success("修改成功",U('index'));
		}
		//显示分类
       	$categoryMolde=new Cate;
	   	$cate=Data::tree($categoryMolde->get(),'cname');
	   	//显示品牌
	   	$brandModel=new Brand;
	   	$brand=$brandModel->get();
	   	View::with('brand',$brand);
	  	View::with('cate',$cate);
		$gid=Q('get.gid',0,'intval');
		$data=$this->model
					->join('goods_detail d','gid','=','d.shop_goods_gid')
					->where("gid={$gid}")->find();
		$data['dimg']=explode('|', $data['dimg']);
		//获取gid代表的属性和规格值
		$goods_attrModel=new Goods_attr;
		$goods_attr=$goods_attrModel->join('type_attr','shop_type_attr_taid','=','taid')
							  ->where("shop_goods_gid={$gid}")
							  ->get();
		//添价空数组，为了方便取出没有设置的属性或者规格，并且能够全部显示在选项表单中
		//不会减少同样分类下的选项					  
		$taid=[];
		foreach($goods_attr as $k=>$v){
			$goods_attr[$k]['tavalue']=explode('|', $v['tavalue']);
			$taid[]=$v['taid'];
		}
		//对数组进行去重
		$taid=array_unique($taid);
		//查找数组之外且shop_goods_gid=$gid的类型属性,查找type_attr表
		$type_attrModel=new Type_attr;
		//将taid转换为字符串
		$taidStr=implode(',', $taid);
		//取出在商品添加时，未选择的属性值
		$tid=$goods_attr[0]['shop_type_tid'];
		$type_attr=$type_attrModel
					->where("taid NOt IN ({$taidStr}) AND shop_type_tid={$tid}")
					->get();
		//进行遍历$type_attr添加到$goods_attr
		if($type_attr){
			foreach($type_attr as $v){
				$goods_attr[]=array(
					'gavalue'=>'',
					'taid'=>$v['taid'],
					'taname'=>$v['taname'],
					'class'=>$v['class'],
					'tavalue'=>explode('|', $v['tavalue']),
					'gaadd'=>''
				);
			}
		}
//		p($goods_attr);
		View::with('goods_attr',$goods_attr);
//		p($data);
		View::with('data',$data);
	    View::make();
	}
	//删除商品
	public function del(){
		$gid=Q('get.gid',0,'intval');
		//删除商品表，先删除图片，再删除数据
		$pic=$this->model->where("gid={$gid}")->find();
		if($pic['pic']){unlink($pic['pic']);}
		$this->model->where("gid={$gid}")->delete();
		//删除商品详情表，先删除图片，再删除数据
		$goods_detailModel=new Goods_detail;
		$dimg=$goods_detailModel->where("shop_goods_gid={$gid}")->find();
		$goods_detailModel->where("shop_goods_gid={$gid}")->delete();
		if($dimg['dimg']){
			$dimg=explode('|', $dimg['dimg']);
			foreach($dimg as $v){unlink($v);}
		}
		//删除商品属性表
		$goods_attrModel=new Goods_attr;
		$goods_attrModel->where("shop_goods_gid={$gid}")->delete();
		View::success('删除成功',U('index'));
		//删除货品属性表
		$goods_list=new Goods_list;
		$goods_list->where("shop_goods_gid={$gid}")->delete();
	}
	//显示货品列表
	public function goodsList(){
		$_SESSION['gid']=Q('get.gid',0,'intval');
		go(U('GoodsList/index'));
	}
}
